

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/egg.png">
  <link rel="icon" href="/img/egg.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="十一的蛋炒饭">
  <meta name="keywords" content="">
  
    <meta name="description" content="1、Maven 简介Maven 官网 Maven 是 Apache旗下的一个开源项目，是一款用于管理和构建java项目的工具。 Apache Maven 基于项目对象模型（POM）的概念，通过一小段描述信息来管理项目的构建。 Maven的作用：  方便的依赖管理：方便快捷的管理项目依赖的资源（jar包），避免版本冲突问题。  统一项目结构：提供标准、统一的项目结构，例如： 123456789- m">
<meta property="og:type" content="article">
<meta property="og:title" content="Maven 笔记1 | 简介、安装、坐标、依赖管理、生命周期">
<meta property="og:url" content="http://example.com/2024/03/10/Maven-%E7%AC%94%E8%AE%B01-%E7%AE%80%E4%BB%8B%E3%80%81%E5%AE%89%E8%A3%85%E3%80%81%E5%9D%90%E6%A0%87%E3%80%81%E4%BE%9D%E8%B5%96%E7%AE%A1%E7%90%86%E3%80%81%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/index.html">
<meta property="og:site_name" content="十一的蛋炒饭的博客">
<meta property="og:description" content="1、Maven 简介Maven 官网 Maven 是 Apache旗下的一个开源项目，是一款用于管理和构建java项目的工具。 Apache Maven 基于项目对象模型（POM）的概念，通过一小段描述信息来管理项目的构建。 Maven的作用：  方便的依赖管理：方便快捷的管理项目依赖的资源（jar包），避免版本冲突问题。  统一项目结构：提供标准、统一的项目结构，例如： 123456789- m">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://uni-pic-bed.oss-cn-hangzhou.aliyuncs.com/blog2024/202403101531768.png">
<meta property="og:image" content="https://uni-pic-bed.oss-cn-hangzhou.aliyuncs.com/blog2024/202403101544909.png">
<meta property="article:published_time" content="2024-03-10T11:04:00.000Z">
<meta property="article:modified_time" content="2024-03-10T11:05:35.278Z">
<meta property="article:author" content="十一的蛋炒饭">
<meta property="article:tag" content="Maven">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://uni-pic-bed.oss-cn-hangzhou.aliyuncs.com/blog2024/202403101531768.png">
  
  
  
  <title>Maven 笔记1 | 简介、安装、坐标、依赖管理、生命周期 - 十一的蛋炒饭的博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.7","typing":{"enable":true,"typeSpeed":60,"cursorChar":"","loop":false,"scope":["home"]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":"§"},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 7.1.1"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>十一的蛋炒饭 的博客</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/" target="_self">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/" target="_self">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/" target="_self">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/" target="_self">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/" target="_self">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/win7.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle">Maven 笔记1 | 简介、安装、坐标、依赖管理、生命周期</span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2024-03-10 19:04" pubdate>
          2024年3月10日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          1.4k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          12 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar category-bar" style="margin-right: -1rem">
    





<div class="category-list">
  
  
</div>


  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">Maven 笔记1 | 简介、安装、坐标、依赖管理、生命周期</h1>
            
              <p id="updated-time" class="note note-info" style="display: none">
                
                  
                    本文最后更新于 2024-03-10T19:05:35+08:00
                  
                  

                
              </p>
            
            
              <div class="markdown-body">
                
                <h2 id="1、Maven-简介"><a href="#1、Maven-简介" class="headerlink" title="1、Maven 简介"></a>1、Maven 简介</h2><p><a target="_blank" rel="noopener" href="http://maven.apache.org/">Maven 官网</a></p>
<p>Maven 是 Apache旗下的一个开源项目，是一款用于<strong>管理和构建java项目的工具</strong>。</p>
<p>Apache Maven 基于项目对象模型（POM）的概念，通过一小段描述信息来管理项目的构建。</p>
<p><strong>Maven的作用：</strong></p>
<ul>
<li><p><strong>方便的依赖管理：</strong>方便快捷的管理项目依赖的资源（jar包），避免版本冲突问题。</p>
</li>
<li><p><strong>统一项目结构：</strong>提供标准、统一的项目结构，例如：</p>
<figure class="highlight ada"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs ada">- maven-project<br> <span class="hljs-comment">-- src</span><br>   <span class="hljs-comment">--- main</span><br>     <span class="hljs-comment">----java</span><br>     <span class="hljs-comment">----resources</span><br>   <span class="hljs-comment">--- test</span><br>     <span class="hljs-comment">----java</span><br>     <span class="hljs-comment">----resources</span><br>   <span class="hljs-comment">--- pom.xml</span><br></code></pre></td></tr></table></figure>
</li>
<li><p><strong>标准的项目构建流程：</strong>标准跨平台（Linux，Windows，MacOS）的自动化项目构建方式。</p>
</li>
</ul>
<p><img src="https://uni-pic-bed.oss-cn-hangzhou.aliyuncs.com/blog2024/202403101531768.png" srcset="/img/loading.gif" lazyload alt="image-20240310153130526"></p>
<p><strong>Maven仓库</strong>：用于存储资源，管理各种 jar 包。</p>
<ul>
<li>本地仓库：自己计算机上的一个目录。</li>
<li>中央仓库：由 Maven 团队维护的全球统一的仓库，仓库地址：<a target="_blank" rel="noopener" href="https://repo1.maven.org/maven2/%E3%80%82">https://repo1.maven.org/maven2/。</a></li>
<li>远程仓库（私服）：一般由公司团队搭建的私有仓库。</li>
</ul>
<h2 id="2、Maven-安装"><a href="#2、Maven-安装" class="headerlink" title="2、Maven 安装"></a>2、Maven 安装</h2><p><strong>环境要求： JDK（注意版本是否兼容）</strong></p>
<p><strong>Maven 安装 一共需要四步操作：</strong></p>
<ol>
<li><strong>从 <a target="_blank" rel="noopener" href="http://maven.apache.org/">Maven 官网 </a>下载zip压缩包，并解压。</strong></li>
<li><strong>配置本地仓库：</strong> 修改 <code>conf/settings.xml</code> 中的 <code>&lt;localRepository&gt;</code> 为一个指定目录。</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">localRepository</span>&gt;</span>E:\maven\rep<span class="hljs-tag">&lt;/<span class="hljs-name">localRepository</span>&gt;</span><br></code></pre></td></tr></table></figure>

<ol start="3">
<li><strong>配置阿里云的镜像：</strong>修改  <code>conf/settings.xml</code> 中的 <code>&lt;mirrors&gt;</code>标签，为其添加如下子标签、</li>
<li><strong>配置环境变量：</strong><code>MAVEN_HOME</code> 为 maven 的解压目录，并将其 bin 目录 加入 <code>PATH</code> 环境变量。</li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">mirrors</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">id</span>&gt;</span>alimaven<span class="hljs-tag">&lt;/<span class="hljs-name">id</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">name</span>&gt;</span>aliyun maven<span class="hljs-tag">&lt;/<span class="hljs-name">name</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">url</span>&gt;</span>http://maven.aliyun.com/nexus/content/groups/public<span class="hljs-tag">&lt;/<span class="hljs-name">url</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">mirrorOf</span>&gt;</span>cental<span class="hljs-tag">&lt;/<span class="hljs-name">mirrorOf</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">mirrors</span>&gt;</span><br></code></pre></td></tr></table></figure>

<p>最后可以在终端中查看是否配置成功：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">mvn -v<br></code></pre></td></tr></table></figure>

<p><img src="https://uni-pic-bed.oss-cn-hangzhou.aliyuncs.com/blog2024/202403101544909.png" srcset="/img/loading.gif" lazyload alt="image-20240310154356343"></p>
<h2 id="3、Maven坐标"><a href="#3、Maven坐标" class="headerlink" title="3、Maven坐标"></a>3、Maven坐标</h2><p><strong>Maven坐标</strong> 是资源的唯一标识，通过该坐标可以唯一定位资源位置。</p>
<p>Maven 坐标主要组成有三个部分：</p>
<ol>
<li><strong>groupId：</strong>定义当前 Maven 项目隶属的组织名称（通常是域名反写，例如：com.bilibili）。</li>
<li><strong>artifactId：</strong>定义当前 Maven 项目名称（通常是模块名称，例如 order-service、goods-service）。</li>
<li><strong>version：</strong>定义当前项目的版本号。</li>
</ol>
<h2 id="4、-Maven-的依赖管理"><a href="#4、-Maven-的依赖管理" class="headerlink" title="4、 Maven 的依赖管理"></a>4、 Maven 的依赖管理</h2><h3 id="4-1-依赖配置"><a href="#4-1-依赖配置" class="headerlink" title="4.1 依赖配置"></a>4.1 依赖配置</h3><p><strong>Maven 中央仓库：</strong><a target="_blank" rel="noopener" href="https://mvnrepository.com/">https://mvnrepository.com/</a></p>
<p><strong>依赖</strong> 是指当前项目运行所需的 jar包，一个项目中可引入多个依赖。</p>
<p><strong>配置方法（4个步骤）：</strong></p>
<ol>
<li><p>在 <code>pom.xml</code> 中 编写<code>&lt;dependency&gt;</code>标签。</p>
</li>
<li><p>在<code>&lt;dependency&gt;</code> 标签中使用 <code>&lt;dependency&gt;</code> 引入坐标。</p>
</li>
<li><p>定义坐标的GAV，即 <code>groupId</code>，<code>artifactId</code>,<code>version</code>。</p>
</li>
<li><p>刷新 Maven 依赖，终端输入命令 <code>mvn dependency:resolve-plugins </code>或者 直接在 IDEA 里点击Maven的刷新按钮。</p>
</li>
</ol>
<figure class="highlight 1c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs 1c">注：<br>  若引入的依赖，在本地仓库不存在，则将会连接到远程仓库或中央仓库下载依赖（可能比较耗时）。<br>  若不知道依赖的坐标信息，可在 Maven中央仓库中搜索 https:<span class="hljs-comment">//mvnrepository.com/</span><br></code></pre></td></tr></table></figure>



<h3 id="4-2-依赖传递"><a href="#4-2-依赖传递" class="headerlink" title="4.2 依赖传递"></a>4.2 依赖传递</h3><p>Maven中的依赖分为<strong>三种</strong>，都具有<strong>传递性</strong>。</p>
<ul>
<li><p><strong>直接依赖：</strong>在当前项目中通过依赖配置建立的依赖关系，比如项目A引入了项目B的依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    	<span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>B<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>artifactB<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br></code></pre></td></tr></table></figure>


</li>
<li><p><strong>间接依赖：</strong>若被依赖的资源依赖其他资源，则当前项目间接依赖其他资源，比如在上面项目A引入了项目B的依赖前，项目B引入了项目C的依赖。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    	<span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>C<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>artifactC<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br></code></pre></td></tr></table></figure>


</li>
<li><p><strong>排除依赖：</strong>排除依赖指主动断开依赖的资源，被排除的资源无需指定版本，比如，项目A引入了项目B的依赖，但是需要排除项目C的依赖。（使用<code>&lt;exclusion&gt;</code>实现）</p>
<p>（注：排除依赖操作不需要标注版本号version）</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span><br>	<span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span><br>    	<span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>B<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>artifactB<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.0<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">exclusions</span>&gt;</span><br>        	<span class="hljs-tag">&lt;<span class="hljs-name">exclusion</span>&gt;</span><br>            	<span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>C<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span><br>                <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>artifactC<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span><br>            <span class="hljs-tag">&lt;/<span class="hljs-name">exclusion</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">exclusions</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span><br></code></pre></td></tr></table></figure></li>
</ul>
<h3 id="4-3-依赖范围"><a href="#4-3-依赖范围" class="headerlink" title="4.3 依赖范围"></a>4.3 依赖范围</h3><p>Maven 项目中引入依赖的 jar 包在默认情况下可在任何地方使用。</p>
<p>而在<code>&lt;dependency&gt;</code>内的<scope>&#96;标签可设置引入依赖的<strong>作用范围</strong>，作用范围有如下3种：</p>
<ol>
<li><strong>主程序范围有效</strong>（<code>main</code> 文件夹范围内）</li>
<li><strong>测试程序范围有效</strong>（<code>test</code> 文件夹范围内）</li>
<li><strong>是否参与打包运行</strong>（<code>package</code>指令范围内）</li>
</ol>
<p><code>scope</code>的取值有如下4种：</p>
<table>
<thead>
<tr>
<th>scope 值</th>
<th>主程序</th>
<th>测试程序</th>
<th>打包（运行）</th>
<th>范例</th>
</tr>
</thead>
<tbody><tr>
<td><code>compile</code>（默认值）</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td><code>log4j</code></td>
</tr>
<tr>
<td><code>test</code></td>
<td>-</td>
<td>√</td>
<td>-</td>
<td><code>junit</code></td>
</tr>
<tr>
<td><code>provided</code></td>
<td>-</td>
<td>√</td>
<td>√</td>
<td>servlet-api</td>
</tr>
<tr>
<td><code>runtime</code></td>
<td>-</td>
<td>√</td>
<td>√</td>
<td>jdbc 驱动</td>
</tr>
</tbody></table>
<h2 id="5、Maven-生命周期"><a href="#5、Maven-生命周期" class="headerlink" title="5、Maven 生命周期"></a>5、Maven 生命周期</h2><p>Maven 的生命周期是为了对所有的 Maven 项目构建过程进行<strong>抽象</strong>和<strong>统一</strong>。</p>
<p>Maven 中有3套<strong>相互独立</strong>的生命周期：</p>
<ol>
<li><strong>clean：</strong>清理工作。</li>
<li><strong>default：</strong>核心工作，如：编译、测试、打包、安装、部署等。</li>
<li><strong>site：</strong>生成报告，发布站点等。</li>
</ol>
<p>其中每套生命周期都包含一些阶段（phase），阶段是有顺序的，后面的阶段依赖于前面的阶段。</p>
<p>其中比较关键的如下（有先后顺序）：</p>
<ul>
<li><code>clean</code> 中的 <code>clean</code> 阶段</li>
<li><code>default</code> 中的 <code>compile</code>阶段、<code>test</code> 阶段、<code>package</code>阶段、<code>install</code>阶段</li>
</ul>
<p>上述5个阶段的作用分别如下：</p>
<ol>
<li><strong>clean ：</strong>移除上一次构建生成的文件。</li>
<li><strong>compile：</strong>编译项目源代码。</li>
<li><strong>test：</strong>使用合适的单元测试框架进行测试。</li>
<li><strong>package：</strong>将编译后的文件打包，如：jar 、war 等。</li>
<li><strong>install：</strong>安装项目到本地仓库。</li>
</ol>
<p><code>注：在同一套生命周期中，当运行后面的阶段时，前面的阶段都会运行。</code></p>
<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h2><p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1m84y1w7Tb">https://www.bilibili.com/video/BV1m84y1w7Tb</a></p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/Maven/" class="print-no-link">#Maven</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>Maven 笔记1 | 简介、安装、坐标、依赖管理、生命周期</div>
      <div>http://example.com/2024/03/10/Maven-笔记1-简介、安装、坐标、依赖管理、生命周期/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>十一的蛋炒饭</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2024年3月10日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2024/03/11/Web%E5%BC%80%E5%8F%91%E7%AC%94%E8%AE%B02-%E5%90%8E%E7%AB%AF-Spring-Boot%E3%80%81HTTP%E5%8D%8F%E8%AE%AE%E3%80%81Web%E6%9C%8D%E5%8A%A1%E5%99%A8%E3%80%81Tomcat/" title="Web 开发笔记2 | 后端 Spring Boot、HTTP协议、Web服务器、Tomcat">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Web 开发笔记2 | 后端 Spring Boot、HTTP协议、Web服务器、Tomcat</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2024/03/07/Git%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B01-Git%E7%AE%80%E4%BB%8B%E3%80%81%E5%AE%89%E8%A3%85%E5%92%8C%E9%85%8D%E7%BD%AE/" title="Git学习笔记1 | Git简介、安装和配置">
                        <span class="hidden-mobile">Git学习笔记1 | Git简介、安装和配置</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>博客网站基于Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>主题Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>





  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  var relativeDate = function() {
    var updatedTime = document.getElementById('updated-time');
    if (updatedTime) {
      var text = updatedTime.textContent;
      var reg = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:Z|[+-]\d{2}:\d{2})/;
      var matchs = text.match(reg);
      if (matchs) {
        var relativeTime = moment(matchs[0]).fromNow();
        updatedTime.textContent = text.replace(reg, relativeTime);
      }
      updatedTime.style.display = '';
    }
  };
  Fluid.utils.createScript('https://lib.baomitu.com/moment.js/2.29.4/moment.min.js', function() {
    if (!'zh-cn'.startsWith('en')) {
      Fluid.utils.createScript('https://lib.baomitu.com/moment.js/2.29.4/locale/zh-cn.min.js', function() {
        relativeDate();
      });
    } else {
      relativeDate();
    }
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
